/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>
     
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core.defaultElements;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Level;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.defaultActions.Move;

public class Player extends Element {

	class Born implements Action {
		public Born() {
	
		}

		public Parameters call(Parameters parameters) {

			Integer e = (Integer) parameters.get("life");
			Player me = (Player) parameters.get("me");
			String subAction = (String) parameters.get("subAction");
			if (subAction == "new" && e != null)
				me.setProperty("energy", e);
			return null;
		}
	}
	class Input implements Action {

		ArrayList<String> currentKeys;
		HashMap<String, Float> directions;
		Parameters info;
		int moving = 0;
		int movingtotal = 3;

		public Input(Element me) {

			directions = new HashMap<String, Float>();
			currentKeys = new ArrayList<String>();
			info = new Parameters();
			info.put("me", me);
			info.put("subAction", "new");
		}

		public Parameters call(Parameters parameters) {

			Player me = (Player) parameters.get("me");
			Level l = (Level) parameters.get("level");
			String subAction = (String) parameters.get("subAction");

			if (subAction == "main") {
				setProperty("isFiring", false);
				boolean onAir;

				Rectangle r = me.getRectangle();
				r.height++;
				onAir = !me.canCollide(new Point(0, 1));

				ArrayList<String> newKeys = new ArrayList<String>();
				newKeys = me.popKeys();

				if (newKeys == null)
					return null;
				Common.info("input handler: " + newKeys.toString());
				Parameters p = new Parameters(info);
				if (newKeys.contains("right"))
					setProperty("direction", 0.0);
				if (newKeys.contains("left"))
					setProperty("direction", 1.0);

				if (newKeys.contains("right") && !currentKeys.contains("right")) {
					p.put("subAction", "new");
					p.put("persist", false);
					p.put("totalTimes", (Integer) getProperty("runTimes"));
					p.put("speed", new Point((Integer) getProperty("runSpeed"),
							0));
					me.actions.runAction("move", p);

				}
				if (!newKeys.contains("right") && currentKeys.contains("right")) {
					// Common.info("input handling 2: "+newKeys.toString() );
					p.put("subAction", "stop");
					me.actions.runAction("move", p);

				}
				if (newKeys.contains("left") && !currentKeys.contains("left")) {
					// Common.info("input handling 3: "+newKeys.toString() );
					p.put("subAction", "new");
					p.put("persist", false);
					p.put("totalTimes", (Integer) getProperty("runTimes"));
					p.put("speed", new Point(
							-(Integer) getProperty("runSpeed"), 0));
					me.actions.runAction("move", p);
				}
				if (!newKeys.contains("left") && currentKeys.contains("left")) {
					// Common.info("input handling 4: "+newKeys.toString() );
					p.put("subAction", "stop");
					me.actions.runAction("move", p);
				}
				if (newKeys.contains("jump") && !currentKeys.contains("jump")) {

					// if(level.canCollide(r, me))
					if (!onAir) {
						Common.info("start jumping");
						p.put("subAction", "new");
						p.put("persist", true);
						p.put("totalTimes", (Integer) getProperty("jumpTimes"));
						p.put("speed", new Point(0,
								-(Integer) getProperty("jumpSpeed")));
						me.actions.runAction("jump", p);
						/*if (((Move) me.actions.getAction("move")).speed.x != 0) {
							Integer z = (int) ((((Double) getProperty("direction")) - 0.5) * (-2));
							p.put("speed", new Point(z, 0));
							Common.info("speed? +"
									+ (Double) getProperty("direction"));
							me.actions.runAction("move", p);
						}*/
					}
				}
							if (newKeys.contains("1") && !currentKeys.contains("1")) {
								Common.info("gun!");
						me.setCurrentGun(0);
					}
									if (newKeys.contains("2") && !currentKeys.contains("2")) {
								Common.info("gun!");
						me.setCurrentGun(1);
					}
									if (newKeys.contains("3") && !currentKeys.contains("3")) {
								Common.info("gun!");
						me.setCurrentGun(2);
					}
				
				if (newKeys.contains("fire")) {
					me.gl.get(me.getCurrentGun()).fire();
					setProperty("isFiring", true);
				}
				currentKeys = newKeys;
			} else if (subAction == "input") {
				ArrayList<String> inputkeys = (ArrayList<String>) parameters
						.get("key");

				keysQueue.add(inputkeys);
				Common.info("input : " + inputkeys.toString());
			}

			return null;
		}

	}

	Parameters info;

	public ArrayList<ArrayList<String>> keysQueue;
	public ArrayList<Gun>  gl;
	public Player() {
		super();
		
		setProperty("energy", new Integer(0));
		setProperty("type", "player");
		setProperty("isRunnable", true);
		setProperty("name", "player");
		setProperty("user", "");
		Sprite s = new Sprite("res/player.png", -1);
		ArrayList<Sprite> ss = new ArrayList<Sprite>();
		ss.add(s);
		setProperty("currentSpriteSet", ss);
		setProperty("runSpeed", 5);
		setProperty("runTimes", 1);
		setProperty("hasGravity", true);
		setProperty("shootSpeed", 10);
		setProperty("jumpSpeed", 5);
		setProperty("jumpTimes", 5);
		setProperty("isFiring", false);
		setProperty("isRunning", false);
		setProperty("canCollide", true);
		setProperty("direction", 0.0); // 0 = right, 1 =left its
										// direction*pigreek angle
		setProperty("direction", 1.0); 
		setProperty("direction", 0.0); 
		setSize(new Point(30, 60));
		actions.addAction("born", new Born());
		actions.addAction("input", new Input(this));
		info = new Parameters();
		info.put("me", this);
		info.put("subAction", "main");
		actions.addAction("jump", new Move());
		actions.addAction("move", new Move());
		keysQueue = new ArrayList<ArrayList<String>>();

		gl = new ArrayList<Gun>();
		setProperty("gunList", gl);


	}


public void setCurrentGun(Integer i)
	{
		Common.info("new gun:"+i);
		setProperty("currentGun", i);

	}
	public int getCurrentGun()
	{
		return (Integer)getProperty("currentGun");

	}	public ArrayList<String> popKeys() {
		if (keysQueue.size() == 0)
			return null;
		ArrayList<String> keys = new ArrayList<String>(keysQueue.get(0));
		keysQueue.remove(0);
		return keys;
	}

	@Override
	public void run() {
		super.run();
		// actions.runAll(stdinfo);

	}

}
